﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using BaoJianSoft.Common;
using BaoJianSoft.Platform.Linq;
using BaoJianSoft.RapidWeb;
using BaoJianSoft.RapidWeb.Controls;
using BaoJianSoft.RapidWeb.DynamicPages;
using BaoJianSoft.RapidWeb.DynamicPages.Configurations;
using System.Threading;
using System.Web.UI;
using System.Text;

namespace BaoJianSoft.Platform.Web.DynamicPage
{
	/// <summary>
	/// Role management dynamic page handler.
	/// </summary>
	public class RoleManagement : BaoJianSoft.RapidWeb.DynamicPages.DynamicPage
	{
		private static object syncObject = new object();
		private static IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
		private static IRoleApi roleApi = SpringContext.Current.GetObject<IRoleApi>();

		/// <summary>
		/// Execute query for results binding to dynamic page grid.
		/// </summary>
		/// <param name="parameter">Query parameter.</param>
		/// <returns>Returns query results.</returns>
		public override QueryResults Query(QueryParameter parameter)
		{
			int recordCount;
			QueryPredicate predicate = parameter.Expressions.Compile();

			string domain = authenticationContext.TempVariables["Domain.Value"] as string;
			Expression<Func<Role, bool>> predicateExpression = role => role.RolesInOrganizationTypes.Any(x => x.OrganizationType.Domain == domain);

			string sortingExpression = null;
			if (parameter.SortExpression != null)
				sortingExpression = parameter.SortExpression.Compile();

			if (Kit.IsEmpty(sortingExpression))
				sortingExpression = "RoleName ASC";

			IEnumerable<RoleObject> roleObjects = roleApi.FindRoles(predicateExpression, predicate.Predicate, sortingExpression, parameter.PageIndex, parameter.PageSize, out recordCount, predicate.Parameters);
			return new QueryResults(recordCount, roleObjects);
		}

		/// <summary>
		/// Delete role by id.
		/// </summary>
		/// <param name="entityId"></param>
		public override void Delete(string entityId)
		{
			RoleObject roleObject = roleApi.Get(new Guid(entityId));
			if (roleObject != null && !roleObject.Predefined)
				roleApi.Delete(roleObject.RoleId);
		}

		/// <summary>
		/// Predefined roles cannot be edited and deleted.
		/// </summary>
		/// <param name="e"></param>
		public override void OnGridRowControlsBind(GridRowControlBindEventArgs e)
		{
			bool predefined = (bool)DataBinder.Eval(e.DataItem, "Predefined");
			e.ShowDeleteButton = !predefined;
			e.ShowEditButton = !predefined;
		}

		/// <summary>
		/// Setup context temporary variables for formatting configured text-typed properties.
		/// Set domain into http context when web page is initializing.
		/// </summary>
		/// <param name="sender">The sender which invokes the method.</param>
		/// <param name="e">Callback event argument.</param>
		public override void SetupContextTempVariables(object sender, SetupApplicationContextVariablesEventArgs e)
		{
			SetupContextTempVariablesUtility.SetupOrganizationDomain(e);
		}
	}
}

